﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.IO;

using Might.Infrastructure.Services;

using Microsoft.Practices.Unity;

namespace Might.Services {

    public class ModalDialog : IModalDialog {
    
        private IUnityContainer container;
        private Popup p;
        private PopupContentView popupContent;
    
        public event ModalDialogEventHandler Closed;
        
        public ModalDialog( IUnityContainer container ) {
            this.container = container;
        }

        public void Show( string caption , string message , ModalDialogButtons buttons ) {                        
            
            p = new Popup();

            popupContent = new PopupContentView();

            popupContent.SetButtons( buttons );
            popupContent.SetText( caption , message );
            popupContent.Click += new EventHandler( content_Click );

            p.Child = popupContent;
            p.IsOpen = true;                        

            p.HorizontalOffset = ( Application.Current.RootVisual as UserControl ).RenderSize.Width / 2 - popupContent.Width / 2;
            p.VerticalOffset = ( Application.Current.RootVisual as UserControl ).RenderSize.Height / 4;            
            
            ( Application.Current.RootVisual as UserControl ).IsEnabled = false;                 
            
        }

        void content_Click( object sender , EventArgs e ) {
            OnClosed( popupContent.Result ); 
        }
        
        protected virtual void OnClosed( ModalDialogResult result ) {
            p.IsOpen = false;
            ( Application.Current.RootVisual as UserControl ).IsEnabled = true;
            if( Closed != null ) {
                Closed( this , result );
            }
        }

    }
    
}
